﻿using System;
using System.Linq;
using System.Threading.Tasks;

namespace Conv
{
    public class Layer:ITeacheble
    {
        public Neuron[,] Neurons { get; protected set; }
        public int Width { get; protected set; }
        public int Height { get; protected set; }
        protected Neuron BiasNeuron = new Neuron(1.0);
        public Mask Msk { get; set; }
        public Layer(int width, int height)
        {
            Height = height;
            Width = width;
            Neurons = new Neuron[Width,Height];
            for (int i = 0; i < Width; i++)
            {
                for (int j = 0; j < Height; j++)
                {
                    Neurons[i, j]= new Neuron(1.0);
                }
            }
        }

        protected Layer(){}
        public void Process()
        {
            foreach (var neuron in Neurons)
            {
                neuron.Process();
            }
        }

        public void SetState(double[,] inputArray)
        {
            for (int i = 0; i < Width; i++)
            {
                for (int j = 0; j < Height; j++)
                {
                    Neurons[i, j].State = inputArray[i, j];
                }
            }
        }
        public void BackPropogate()
        {
            /*ParallelOptions p = new ParallelOptions();
            p.MaxDegreeOfParallelism = Environment.ProcessorCount;
            Parallel.For(0, Width, p, n =>
                                          {
                                              for (int i = 0; i < Height; i++)
                                              {
                                                  Neurons[n, i].BackPropogate();
                                              }
                                          }); */
            foreach (Neuron neuron in Neurons)
            {
                neuron.BackPropogate();
            }
        }

        public void UpdateWeight()
        {
            /*ParallelOptions p = new ParallelOptions();
            p.MaxDegreeOfParallelism = Environment.ProcessorCount;
            Parallel.For(0, Width, p, n =>
            {
                for (int i = 0; i < Height; i++)
                {
                    Neurons[n, i].UpdateWeight();
                }
            }); */
            foreach (Neuron neuron in Neurons)
            {
                neuron.UpdateWeight();
            }
        }
    }
}